var selectedRow = '';
$("#dataTable1").on("click-row.bs.table",function(e,row,ele){
    $("#dataTable1 .success").removeClass("success");
    $(ele).addClass("success");
    selectedRow = row;
});

window.actionEvents = {
    'click .infoView': function(e, value, row, index) {
        //编辑操作
        selectedRow = row;
    },
}

function getParams(params) {
    var sicknessName = $("#sicknessName").val();
    var twoLevelDeptCode = $("#twoLevelDeptCode").val();
    var oneLevelDeptCode=$("#oneLevelDeptCode").val();
    var pageNo = params.pageNumber;
    var pageSize = params.pageSize;
    return {
        sicknessName : sicknessName,
        oneLevelDeptCode : oneLevelDeptCode,
        twoLevelDeptCode : twoLevelDeptCode,
        pageSize : pageSize,
        pageNo : pageNo
    };
}

function doRefresh(){
    $('#dataTable1').bootstrapTable('refresh', {
        url: basePath + 'basicSicknessInfo/pageFindSicknessData?t=' + new Date().getTime()
    });
}


function openAddSickPage(){
    var tableOptions = $('#dataTable1').bootstrapTable('getOptions', null);
    window.location.href = basePath + 'basicSicknessInfo/editSick?sicknessName='+$("#sicknessName").val()+'&oneLevelDeptCode='+$("#oneLevelDeptCode").val()+'&twoLevelDeptCode='+$("#twoLevelDeptCode").val()
        +'&t=' + new Date().getTime() + '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}

function openEditSickPage(){
    var tableOptions = $('#dataTable1').bootstrapTable('getOptions', null);
    if(selectedRow == ''){
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    window.location.href = basePath + 'basicSicknessInfo/editSick?sicknessCode='+selectedRow.sicknessCode+'&id='+selectedRow.id+'&sicknessName='+$("#sicknessName").val()
        +'&oneLevelDeptCode='+$("#oneLevelDeptCode").val()+'&twoLevelDeptCode='+$("#twoLevelDeptCode").val()+'&t=' + new Date().getTime()+ '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}


function doExport(){
    $('#searchForm').attr("action", basePath + "basicSicknessInfo/exportSicknessExcel");
    $('#searchForm').submit();
}

function checkSickDelete() {
    if(selectedRow == ''){
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    var str = '是否删除疾病【'+selectedRow.sicknessName+'】?';
    swalToastr('操作提示', str, 'warning',doDeleSickness , '确定');
}

function doDeleSickness(){
    $.ajax({
        type: 'post',
        url:  basePath + 'basicSicknessInfo/deleSick?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify({
            id: selectedRow.id,
            sicknessCode: selectedRow.sicknessCode,
        }),
        success: function (data) {
            if (data.result) {
                toastr.success(data.message);
                doRefresh();
                //window.location.href = basePath + 'basicSicknessInfo/sicknessPage?t=' + new Date().getTime();
            } else {
                toastr.error(data.message);
            }
        },
        error: function (request) {
            hideLoading();
            toastr.error(common_system_error);
        }
    });
}


function doImportSickExcel() {
    showLoading();
    $.ajaxFileUpload({
        type: 'post',
        url: basePath + 'basicSicknessInfo/excelImportSicknessData',
        dataType: 'json',
        secureuri : false,
        fileElementId : ["excelData"],
        contentType : "application/json; charset=UTF-8",
        success : function (data) {
            hideLoading();
            doRefresh();
            if(data && data.result){
                //操作成功
                if(data.content) {
                    if (data.content.successNum == 0) {
                        toastr.error('没有可导入数据');
                        showImportMessage(data.content);
                    } else {
                        toastr.success('批量导入成功');
                        showImportMessage(data.content);
                    }

                }
            }else{
                toastr.error('导入失败');
            }
        },
        error: function (request) {
            hideLoading();
            delayToastr('系统错误');
            doRefresh();
        }
    });
}

function showImportMessage(data) {
    if (!data) return;
    $('#importResultLabel').html(data.importTip);
    if(data.failNum != 0){
        $('#errorTable').bootstrapTable('load', {
            data : data.errorData
        });$('#_errorTipDiv').removeClass('div-hidden');

    }
    $('#_ImportExcelDiv').removeClass('modal-body-height-new');
}


function doImport(){
    $("#excelData").val('');
    $("#fileName").html('');
    $('#_importTip').html('');
    $('#importResultLabel').html('');
    $('#_errorTipDiv').addClass('div-hidden');
    //$("#excelNameShow").html('');
    $("#importPage").modal('show');
}

function downTemplate() {
    $('#dataForm').attr("action", basePath + "basicSicknessInfo/getSicknessExcelTemplate");
    $('#dataForm').submit();
}

function fileChangeEvent(obj) {
    var id = $(obj).attr('id');
    var f = document.getElementById(id).files;
    if (!f) {
        return true;
    }
    var name = f[0].name;
    $("#fileName").html(name);
}


function showList() {
    if(selectedRow == ''){
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    $.ajax({
        type: 'post',
        url: basePath + 'basicSicknessInfo/openListPage?&id='+selectedRow.id+'&sicknessCode='+selectedRow.sicknessCode+ '&t=' + new Date().getTime(),
        dataType: 'json',
        contentType : "application/json; charset=UTF-8",
        success : function (data) {
            if(data.result){
                $("#qrcode").html("");
                var content = data.content;
                initQrCode(content.qrUrl);
                $("#_validNum").html(content.validNum);
                $("#scanQrCodePage").modal('show');
            }else{
                toastr.error(data.message);
            }
        }, error: function (request) {
            toastr.error(common_system_error);
        }
    });
}


function getTwoDeptInfoList(value){
    $("#twoLevelDeptCode").val('');
    if(value == null){
        value = '';
    }
    doSickRefresh();
    $.ajax({
        type: 'post',
        url: basePath + 'basicSicknessInfo/getTwoDeptInfoList?oneDeptCode=' + value + '&t=' + new Date().getTime(),
        dataType: 'json',
        success : function (data) {
                var htmlStr = '<option value="">--请选择--</option>';
                if (data.content) {
                    for (var i=0; i<data.content.length; i++) {
                        htmlStr += '<option value="'+data.content[i].deptCode+'">'+data.content[i].deptName+'</option>';
                    }
                }
                $("#twoLevelDeptCode").html(htmlStr);
        }, error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function doSickRefresh() {
    $('#dataTable1').bootstrapTable('refresh', {
        url: basePath + 'basicSicknessInfo/pageFindSicknessData?&t='+new Date().getTime(),
    });
}